/**
 * 
 */
package com.cipe.cmrs.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cipe.cmrs.bean.PayeeBean;
import com.cipe.cmrs.model.Country;
import com.cipe.cmrs.model.Payee;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.util.DateUtil;

/**
 * @author Incepio
 * 
 */
@Service
@Transactional
public class PayeeService extends BaseService<Payee> {

	@Autowired
	private UserService	userService;
	@Autowired
	private ConversionService conversionService;

    public Payee savePayee(Payee payee, Integer userId) {
        if (payee != null) {
            User user = userService.findById(userId);
            Payee dbPayee = findById(payee.getId());
            if (dbPayee == null) {
                dbPayee = new Payee();
                dbPayee.setCreatedBy(user);
                dbPayee.setCreationDate(DateUtil.getCurrentDate());
            }
            dbPayee.setPayeeName(payee.getPayeeName());
            dbPayee.setPayeeAddress1(payee.getPayeeAddress1());
            dbPayee.setPayeeAddress2(payee.getPayeeAddress2());
            dbPayee.setPayeeCity(payee.getPayeeCity());
            if (payee.getPayeestate() != null && payee.getPayeestate().getId() != null && payee.getPayeestate().getId() > 0) {
                dbPayee.setPayeestate(payee.getPayeestate());
            } else {
                dbPayee.setPayeestate(null);
            }
            dbPayee.setPayeeZipCode(payee.getPayeeZipCode());
            Country payeeCountry = payee.getPayeeCountry();
            if (null != payeeCountry && payeeCountry.getId() != null && payeeCountry.getId() > 0) {
                dbPayee.setPayeeCountry(payeeCountry);
            } else {
                dbPayee.setPayeeCountry(null);
            }
            dbPayee.setBeneficiarytName(payee.getBeneficiarytName());
            dbPayee.setBeneficiaryAddressLine1(payee.getBeneficiaryAddressLine1());
            dbPayee.setBeneficiaryAddressLine2(payee.getBeneficiaryAddressLine2());
            dbPayee.setBeneficiaryCity(payee.getBeneficiaryCity());
            dbPayee.setBeneficiaryState(payee.getBeneficiaryState());
            dbPayee.setBeneficiaryZipCode(payee.getBeneficiaryZipCode());
            // Country beneficiaryCountry = payee.getBeneficiaryCountry();
            // if (null != beneficiaryCountry && beneficiaryCountry.getId() !=
            // 0) {
            // dbPayee.setBeneficiaryCountry(beneficiaryCountry);
            // }
            dbPayee.setBeneficiaryPhoneNumber(payee.getBeneficiaryPhoneNumber());
            dbPayee.setBankName(payee.getBankName());
            dbPayee.setBankCode(payee.getBankCode());
            dbPayee.setChipsCode(payee.getChipsCode());
            dbPayee.setAccountNumber(payee.getAccountNumber());
            dbPayee.setBankAddressLine1(payee.getBankAddressLine1());
            dbPayee.setBankAddressLine2(payee.getBankAddressLine2());
            dbPayee.setBankCity(payee.getBankCity());
            dbPayee.setBankZipCode(payee.getBankZipCode());
            dbPayee.setBankState(payee.getBankState());
            // Country bankCountry = payee.getBankCountry();
            // if (null != bankCountry && bankCountry.getId() != 0) {
            // dbPayee.setBankCountry(bankCountry);
            // }
            dbPayee.setIntermediaryBank(payee.getIntermediaryBank());
            dbPayee.setIntermediaryBankCode(payee.getIntermediaryBankCode());
            dbPayee.setSpecialInstructions(payee.getSpecialInstructions());
            dbPayee.setPayeetype(payee.getPayeetype());
            dbPayee.setInactive(payee.getInactive());
            dbPayee.setUpdatedBy(user);
            dbPayee.setUpdationDate(DateUtil.getCurrentDate());
            return merge(dbPayee);
        }
        return null;
	}
	
	public List<Payee> getAllActivePayees() {
        return dao.getResultList("FROM Payee WHERE inactive IS NULL or inactive != ?1 ORDER BY payeeName ASC",
            new Boolean[]{ Boolean.TRUE });
	}
	
    @Transactional(readOnly = true)
    public PayeeBean getPayee(Integer id) {
        return conversionService.convert(dao.findById(Payee.class, id), PayeeBean.class);
    }
}
